package com.hbt.feature_provider.service;

import com.hbt.api.entity.Feature;
import com.hbt.api.entity.WordFea;
import com.hbt.feature_provider.dao.FeatureMapper;
import com.hbt.feature_provider.dao.WordFeaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FeatureServiceImpl implements FeatureService{
    @Autowired
    FeatureMapper featureMapper;
    @Autowired
    WordFeaMapper wordFeaMapper;
    public int changeSysFeatures(List<String> features, int userid) {
        System.out.println("enter feature provider service changeSysFeatures");
        //在sys_feature表中先删除具有userid的值的行
        featureMapper.delAllSysFeatureByUserid(userid);
        //将features中的数据一条一条地添加到sys_feature表中
        int count = 0;
        for (String feature : features){
            featureMapper.addSysFeature(feature, userid);
            count++;
        }
        return count;
    }

    public List<String> getSysFeatures(int userid) {
        System.out.println("enter service getSysFeatures");
        return featureMapper.getSysFeatures(userid);
    }

    public int addFeature(String descript, int wordid) {
        //从数据库中查找feature表是否存在descript的行
        //若有，则封装为Feature， 若没有则插入并封装为Feature，
        int feature_id = 0;
        Feature f = new Feature();
        f.setDescript(descript);
        f = featureMapper.getFeature(f);
        if (f != null) {
            feature_id = f.getId();
        } else {
            Feature f1 = new Feature();
            f1.setDescript(descript);
            featureMapper.insertFeature(f1);
            feature_id = f1.getId();
        }

        //通过Feature的id和wordid插入到w_f表中
        WordFea wordFea = new WordFea();
        wordFea.setFeatureid(feature_id);
        wordFea.setWordid(wordid);
        return wordFeaMapper.insertWordFea(wordFea);
    }

    public int delFeature(String descript, int wordid) {
        //通过descript获取featureid
        Feature f = new Feature();
        f.setDescript(descript);
        f = featureMapper.getFeature(f);
        //通过featureid和wordid删除w_f中的行
        WordFea wordFea = new WordFea();
        wordFea.setWordid(wordid);
        wordFea.setFeatureid(f.getId());
        return wordFeaMapper.deleteWordFea(wordFea);
    }
}
